package edu.hubu.supermarketmanagementsystem.controller;

import edu.hubu.supermarketmanagementsystem.entity.Product;
import edu.hubu.supermarketmanagementsystem.service.ProductService;
import edu.hubu.supermarketmanagementsystem.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    // 添加商品
    @PostMapping("/add")
    public ResultVO<String> addProduct(@RequestBody Product product) {
        return productService.addProduct(product);
    }

    // 删除商品
    @DeleteMapping("/delete/{productId}")
    public ResultVO<String> deleteProduct(@PathVariable int productId) {
        return productService.deleteProductById(productId);
    }

    // 更新商品
    @PutMapping("/update")
    public ResultVO<String> updateProduct(@RequestBody Product product) {
        return productService.updateProduct(product);
    }

    // 根据ID查询商品
    @GetMapping("/get/{productId}")
    public ResultVO<Product> getProductById(@PathVariable int productId) {
        return productService.getProductById(productId);
    }

    // 根据名称模糊查询
    @GetMapping("/search")
    public ResultVO<List<Product>> searchProductsByName(@RequestParam String name) {
        return productService.searchProductsByName(name);
    }

    // 分页查询所有商品
    @GetMapping("/list")
    public ResultVO<List<Product>> getAllProducts(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        return productService.getAllProducts(page, pageSize);
    }


    // 按类别筛选商品
    @GetMapping("/category")
    public ResultVO<List<Product>> getProductsByCategory(@RequestParam String category) {
        return productService.getProductsByCategory(category);
    }

    // 更新库存
    @PatchMapping("/updateStock")
    public ResultVO<String> updateStock(@RequestParam int productId, @RequestParam int stockDelta) {
        return productService.updateStock(productId, stockDelta);
    }
}
